<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="servlets.*"%>
<%@ page import="org.hibernate.SessionFactory"%>
<%@ page import="org.hibernate.Session"%>
<%@ page import="java.util.List"%>
<%@ page import="hibernate.model.*"%>
<%@ page import="hibernate.util.HibernateUtil"%>
<%@ page import="hibernate.method.*"%>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css" media="screen">
/*====================================================
	- HTML Table Filter stylesheet
=====================================================*/
@import "../management/css/filtergrid.css";

/*====================================================
	- General html elements
=====================================================*/
body {
	margin: 15px;
	padding: 15px;
	border: 1px solid #666;
}

h2 {
	margin-top: 50px;
}

caption {
	margin: 10px 0 0 5px;
	padding: 10px;
	text-align: left;
}

pre {
	font-size: 13px;
	margin: 5px;
	padding: 5px;
	background-color: black;
	border: 1px solid #ccc;
}

.mytable {
	width: 95%;
	font-size: 12px;
	border: 1px solid #ccc;
	color: white;
}

div.tools {
	margin: 5px;
}

div.tools input {
	background-color: black;
	border: 2px outset #f4f4f4;
	margin: 2px;
	color: white;
}

th {
	background-color: #003366;
	color: #FFF;
	padding: 2px;
	border: 1px solid #ccc;
}

td {
	padding: 2px;
	border-bottom: 1px solid #ccc;
	border-right: 1px solid #ccc;
}
</style>
<script type="text/javascript"
	src="../management/js/jquery.tablesorter.js">
	
</script>
<script type="text/javascript" src="../management/js/actb.js"></script>
<!-- External script -->
<script type="text/javascript" src="../management/js/tablefilter.js"></script>
<script>
	$("a.users").click(
			function() {
				$("#users_content").hide().fadeIn(1000).load(
						"users/userProfile.jsp?user_id=" + $(this).attr('id'));
				return false;
			});
</script>
<%
	SessionFactory sessionFactory = HibernateUtil
						.getSessionAnnotationFactory();
				Session s = sessionFactory.openSession();
				s.beginTransaction();
				UserMethods um = new UserMethods();
					response.setContentType("text/html");
					List<User> users = um.list(sessionFactory, s);
%>
<div class="page-header">
	<h2 class="page-title">KATALOG UZYTKOWNIKOW</h2>
</div>
<div id="content">
	<br />

	<%
		if (users.size() > 0) {
		int counter =1;
	%>
	<table id="table6" class="mytable">
		<thead>
			<tr>
				<th style="width: 20px;">Lp</th>
				<th style="width: 150px">Nazwa uzytkownika</th>
				<th>Typ</th>
				<th>Usuń</th>
			</tr>
		</thead>
		<%
			for (User g1  : users) {
				//	if (g1==um.id_to_user(sessionFactory, s, (Integer) session.getAttribute("user_id"))) 
			//{
			//	int toRemove=users.indexOf(g1);
			//	System.out.println(toRemove);
			//}
			//else {
				int user_id = g1.getUser_id();
				String userName = g1.getUsername();
		%>

		<tbody>
			<tr>
				<td><%=counter%></td>
				<td>
					<%
						out.println("<a class=\"users\" id=\""+ user_id + "\" href=\"#\">" + userName + "</a>");
					%>
				</td>
				<td>
					<form action="../ChangeType" method="POST" name='formularz'>
						<select id="type" name="type" required>



							<%
								if("worker".equals(g1.getType())){
							%>
							<option value="client">klient</option>
							<option value="worker" selected>pracownik</option>
							<option value="admin">administrator</option>
							<%
								}
								 else if ("client".equals(g1.getType())){
							%>
							<option value="client" selected>klient</option>
							<option value="worker">pracownik</option>
							<option value="admin">administrator</option>
							<%
								}
								 else if ("admin".equals(g1.getType())) {
							%>
							<option value="client">klient</option>
							<option value="worker">pracownik</option>
							<option value="admin" selected>administrator</option>
							<%
								}
							%>
						</select> <input type="hidden" name="user_id" id="user_id"
							value="<%=g1.getUser_id()%>"> <input type="submit"
							class="mainBtn" value="Zmień"> <br />
					</form>
				</td>
				<td>
					<form action="../DeleteUser" method="POST" name='formularz'>
						<input type="hidden" name="user_id" id="user_id"
							value="<%=g1.getUser_id()%>"> <input type="submit"
							class="mainBtn" value="Usuń"> <br />
					</form>
				</td>


			</tr>
			<%
				counter++;
			//	}
				}
					
					} else out.println("Nie ma zadnych uzytkownikow w bazie oprocz ciebie");
					s.getTransaction().commit();
			%>

		</tbody>
	</table>
	<script>
		$(document).ready(function() {
			$(".mytable").tablesorter({
				debug : true
			});
		});
	</script>
	<br> <br>


	<script type="text/javascript">
		//<![CDATA[
		var table6_Props = {
			//paging: true,
			//paging_length: 3,
			rows_counter : true,
			btn_reset : true,
			loader : true,
			loader_text : "Filtering data..."
		};
		setFilterGrid("table6", table6_Props);
		//]]>
	</script>
</div>


